capture log close 
log using repression.log, replace

***************************************************
***   Are coups good for democracy? 		***
***						***
*** 	author: jgw 				***
***	origin date: 8.12.15 			***
***	last updated: 8.17. 15 			***
***						***
***	using files:				***
***		fariss_original.dta		***
***		GWF_AllPoliticalRegimes.dta 	***
***		powell_thyne_coups_final.txt	***
***		GWFtscs2015.dta			***
***		clean-dict-dict-coups.dta	***
***		f1.csv				***
***						***
***************************************************

*****************************************************************************************
*											*
* \copyright   2015 Joseph Wright \\							*
*											*
* This research is funded by the National Science Foundation BCS-0904463		*
*											*
* This program is free software: you can redistribute it and/or modify			*
*    it under the terms of the GNU General Public License as published by		*
*    the Free Software Foundation, version 3. \\					*
*											*
* This program is distributed in the hope that it will be useful,			*
*   but WITHOUT ANY WARRANTY; without even the implied warranty of			*
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the			*
*   GNU General Public License for more details.\\					*
*											*
* See  http://www.gnu.org/licenses/ for a copy of the GNU General Public License. 	*    
* 											*
*****************************************************************************************

set more off
********************************
* Merge data, create variables *
********************************
		* Get repression data *
cd "C:\Users\jwright\Documents\My Dropbox\Research\Coups\DFGW\Data files\annual data analysis"
use fariss_original, clear  /* downloaded on 9.30.2014 from Dataverse HumanRightsProtectionScores_v2.03.csv  */
rename latentmean repression
replace repress = repress*-1 /* flip scale so higher values are more repression */
hist repress, scheme(lean2)
recode cow (679=678)
sort cow year
drop if year==year[_n-1] & cow==cow[_n-1]
tsset cow year
rename cow cowcode
keep cow year repression
sort cow year
		* Merge in All political regimes from GWF *
cd "C:\Users\jwright\Documents\My Dropbox\Research\Coups\DFGW\Data files\annual data analysis"
merge cow year using GWF_AllPoliticalRegimes   /* 1946-2010 */
tab _merge
rename _merge merge1
browse cow gwf_case year if merge1==2 & gwf_duratio!=.  /* gwf observation with no repression data */
gen lnd =ln(gwf_duration)
tab gwf_fail if gwf_non~="NA"
list cow gwf_case year if year~=. & gwf_fail==1 & gwf_non=="NA" & gwf_next=="", clean
replace gwf_next = "democracy" if (cow==265 & year==1990) |  (cow==365 & year==1991) | (cow==438 & year==2010) | (cow==703 & year==2010) /* East Germany, Soviet Union, Kyrgyzstan, Guinea */
gen dictfaildem = gwf_fail==1 & gwf_non=="NA" & (gwf_next=="democracy" | gwf_next=="provisional") if gwf_fail~=.
gen dictfaildict = gwf_fail==1 & gwf_non=="NA" & (gwf_next~="democracy" & gwf_next~="provisional") if gwf_fail~=. 
tab gwf_fail dictfaildem if gwf_fail==1 & gwf_non=="NA" 
tab gwf_fail dictfaildict if gwf_fail==1 & gwf_non=="NA"
sort cow year
save temp, replace
		* Get coups *
insheet using http://www.uky.edu/~clthyn2/coup_data/powell_thyne_coups_final.txt, clear
rename ccode cowcode
rename country pt_country
* 1st coup in Yemen (ccode=680) 1968 is South Yemen but the second coup in August is actually in N/All-Yemen
recode cow (679=678)
recode cow (680=678) if year==1968 & month==8
gen  p = "/"
egen d = concat(day p month p year)
gen date  = date(d, "DMY")
gen coupA = coup==1
gen coupS = coup==2
drop p d
sort cow year
merge cow year using GWFtscs2015
gen enddate = date(gwf_enddate, "DMY")
tab _merge
rename _merge merge2
* no coups in Oman or East Germany *
recode coup* (.=0) 
gen gwf_endmonth = month(enddate)
gen gwf_endday = day(enddate)

* drop coups that occur in same calendar year as collapse but in a later month *
recode coup* (1=0) (2=0) if gwf_endmonth< month & gwf_fail==1 
* coups in PT that occur in same month as GWF regime collapse *
list gwf_case gwf_fail_type year month day coupA coupS gwf_enddate if gwf_fail==1 & coup~=0 & coup~=.& /*
*/ gwf_endmonth==month & gwf_endday~=day, clean
 
* Monthly *
egen sumcoup = sum(coup), by(cow month year)
gen anycoup = sumcoup>0 if sumcoup~=.
egen sumcoupA = sum(coupA), by(cow month year)
gen anycoupA = sumcoupA>0 if sumcoup~=.
egen sumcoupS = sum(coupS), by(cow month year)
gen anycoupS = sumcoupS>0 if sumcoup~=.
egen tag = tag(cow month year)
keep if tag==1
keep cow month year sum* any* pt_country
sort cow year month
*save coup_monthly, replace
*Yearly*
local var  = "sumcoup anycoup sumcoupA anycoupA sumcoupS anycoupS"
foreach l of local var {
rename `l' x`l'
}
egen sumcoup = sum(xsumcoup), by(cow year)
gen anycoup = sumcoup>0 if sumcoup~=.
egen sumcoupA = sum(xsumcoupA), by(cow year)
gen anycoupA = sumcoupA>0 if sumcoup~=.
egen sumcoupS = sum(xsumcoupS), by(cow year)
gen anycoupS = sumcoupS>0 if sumcoup~=.
egen tag = tag(cow year)
keep if tag==1
keep cow year sum* any* pt_country
local var  = "sumcoup anycoup sumcoupA anycoupA sumcoupS anycoupS"
foreach l of local var {
rename `l' pt_`l'
}
sort cow year
		* Merge into temp data *
cd "C:\Users\jwright\Documents\My Dropbox\Research\Coups\DFGW\Data files\annual data analysis"
merge cow year using temp
tab _merge 
rename _merge merge3
recode pt_sum* pt_any* (.=0) if year>1949

* Variable construction *
	* Include coups that occur under Jan 1 autocracy *
gen Dcoup = pt_anycoupS==1 & dictfaildem==1 if gwf_fail~=. & gwf_non=="NA"
gen Acoup = pt_anycoupS==1 & dictfaildict==1 if gwf_fail~=. & gwf_non=="NA"
gen Ncoup = gwf_fail==0 & pt_anycoupS==1 if gwf_fail~=.  & gwf_non=="NA"
gen Fcoup = pt_anycoupA==1 & pt_anycoupS==0 if gwf_fail~=.  & gwf_non=="NA"
tab pt_anycoupS Dcoup
tab pt_anycoupS Acoup
tab pt_anycoupS Ncoup
tsset cow year
tssmooth ma Dwindow =Dcoup, window(1 1 1)    
tssmooth ma Awindow =Acoup, window(1 1 1)   
tssmooth ma Nwindow =Ncoup, window(1 1 1)
tssmooth ma Fwindow =Fcoup, window(1 1 1)
local var = "Dwindow Awindow Nwindow Fwindow" 
foreach l of local var  {
	replace `l' = `l'>0 if `l'~=.
}
local var = "D A N F" 
foreach l of local var  {
	tssmooth ma post`l'coup = `l'coup, window(1 0 0)
	tssmooth ma pre`l'coup = `l'coup, window(0 0 1)
	recode `l'coup post`l'coup pre`l'coup (.=0) /* sample always based on window not missing for NA */
}
gen postcoup = postDcoup==1 | postAcoup==1 | postNcoup==1 | postFcoup==1
gen precoup = preDcoup==1 | preAcoup==1 | preNcoup==1 | preFcoup==1

gen period = year<=1955
replace period=2 if year<=1960 & year>1955
replace period=3 if year<=1965 & year>1960
replace period=4 if year<=1970 & year>1965
replace period=5 if year<=1975 & year>1970
replace period=6 if year<=1980 & year>1975
replace period=7 if year<=1985 & year>1980
replace period=8 if year<=1990 & year>1985
replace period=9 if year<=1995 & year>1990
replace period=10 if year<=2000 & year>1995
replace period=11 if year<=2005 & year>2000
replace period=12 if year>2005

keep if year<2011 & year>=1950
save temprepress, replace 


******************************************************
*** Statistic on coups in democracy by time period ***
******************************************************
use temprepress, clear
tab gwf_non if pt_anycoupS==1 & gwf_non~="warlord" & gwf_non~="not-independent" &  year<1990 /* 23% in democracies */
tab gwf_non if pt_anycoupS==1 & gwf_non~="warlord" & gwf_non~="not-independent" &  year>1989 /* 45% in democracies */

***************************************
**Verify with hand-cleaned coup data **
***************************************	
		* clean dict-dict coups with no other coup activity in the coupwindow *
use clean-dict-dict-coups, clear
tab pt_anycoup
gen coldwar = year<1990
reg repression postcoup precoup, cluster(gwf)
lincom postcoup-precoup
reg repression postcoup precoup coldwar, cluster(gwf)
lincom postcoup-precoup
xi:reg repression i.coldwar*postcoup i.coldwar*precoup , cluster(gwf)
lincom postcoup-precoup
**************
** Analysis **
**************
	* 1950 - 2010 *
		use temprepress, clear
		qui: xi: xtreg repress i.period postcoup precoup  if  /*
		*/(Dwindow==1 | Awindow==1 | Nwindow==1 | Fwindow==1),fe i(cow) cluster(cow)
		egen m = count(cow) if e(sample), by(cow)
		tab m
		tab pt_anycoup if e(sample)
		drop if m<3 
		sort cow year
		save temprepress, replace
		
		qui: xi: xtreg repress i.period postcoup precoup  if m>=3 & /*
		*/(Dwindow==1 | Awindow==1 | Nwindow==1 | Fwindow==1),fe i(cow) cluster(cow)
		lincom post-pre
		tab pt_anycoupS if e(sample)
		est store repress1
		qui: xi: xtreg repress i.period postDcoup preDcoup  if m>=3 & /*
		*/(Dwindow==1),fe i(cow) cluster(cow)
		lincom postD-preD
		tab Dcoup if e(sample)
		est store repress2
		qui: xi: xtreg repress i.period postAcoup preAcoup  if m>=3 & /*
		*/(Awindow==1),fe i(cow) cluster(cow)
		lincom postA-preA
		tab Acoup if e(sample)
		est store repress3
		qui: xi: xtreg repress i.period postNcoup preNcoup  if  m>=3 & /*
		*/(Nwindow==1),fe i(cow) cluster(cow)
		lincom postN-preN
		tab Ncoup if e(sample)
		est store repress4
		qui: xi: xtreg repress i.period postFcoup preFcoup  if m>=3 &  /*
		*/(Fwindow==1),fe i(cow) cluster(cow)
		lincom postF-preF
		tab Fcoup if e(sample)
		est store repress5

		tempfile g1 g2 g3 g4
		estimates restore repress5
		nlcom _b[postFcoup]-_b[preFcoup], post level(95) 
		parmest,label saving(`g1',replace) level(95)
		estimates restore repress3
		nlcom _b[postAcoup]-_b[preAcoup], post level(95) 
		parmest,label saving(`g2',replace) level(95) 
		estimates restore repress2
		nlcom _b[postDcoup]-_b[preDcoup], post level(95) 
		parmest,label saving(`g3',replace) level(95) 
		estimates restore repress4
		nlcom _b[postNcoup]-_b[preNcoup], post level(95) 
		parmest,label saving(`g4',replace) level(95) 
		dsconcat `g1' `g2' `g3' `g4'
		replace parm="F" if _n==1
		replace parm="A" if _n==2
		replace parm="D" if _n==3
		replace parm="N" if _n==4
		sencode parm, gen(myparm)
		drop if _n>4
		replace estimate = estimate*100
		replace min95 = min95*100
		replace max95 = max95*100
		gen est = round(estimate, .1)
		eclplot estimate min max myparm if _n<=4, rplottype() title(1950-2010, size(medlarge)) /*
		*/ yline(0,lcolor(gs10) lpattern(dash)) xscale(range(0.75 4.25))  /*
		*/ xlab(1 2 3 4) xtitle("Political change after coup event",height(6)) ytitle("(Post-coup) - (Pre-coup) repression") /*
		*/ plotregion(margin(large)) estopts(mlabel(est) mlabposition(8)) scheme(lean2) ylab(-40 (20) 40,glcolor(gs16)) /*
		*/ legend(on lab(1 "95% ci") lab (2 "estimate") pos(6) ring(2) col(2) size(small)) saving(t1, replace)
 		estout repress* using C1.tex, cells(b(star  fmt(%9.3f)) se(par fmt(%9.2f))) stats(ll r2 N) style(tex) replace label starlevels(+ 0.10 * 0.05 ** 0.01)

	* 1990 - 2010 *
		use temprepress, clear
		* list successful coups: 1990-2010 *
		tab pt_anycoupS if gwf_duration~=. & year>1989  /* 16 coups should mean 48 observations using a three-year window */
		sort gwf_country year
		listtex gwf_country year Dwindow Awindow Nwindow if year>1989 &(Dwindow==1 | Awindow==1 | /*
		*/ Nwindow==1) & pt_anycoupS==1 using couplist.tex,  rstyle(tabular) /*
		*/ head("\begin{tabular}{l c c c c}  \textit{Country} &\textit{Year}&\textit{Democratization}&\textit{Autocratic transtion}&\textit{No change} \\\\") /*
		*/ foot("\end{tabular}") replace
		list gwf_case cow year repress if Awindow==1 & year>1989 & postAcoup==1, clean
		list gwf_case cow year repress if Awindow==1 & year>1989 & preAcoup==1, clean  

		qui: xi: xtreg repress i.period postcoup precoup  if  year>1989 & /*
		*/(Dwindow==1 | Awindow==1 | Nwindow==1 | Fwindow==1),fe i(cow) cluster(cow)
		lincom post-pre
		tab pt_anycoupS if e(sample)
		est store repress1
		qui: xi: xtreg repress i.period postDcoup preDcoup  if year>1989 & /*
		*/(Dwindow==1),fe i(cow) cluster(cow)
		lincom post-pre
		tab Dcoup if e(sample)
		est store repress2
		qui: xi: xtreg repress i.period postAcoup preAcoup  if  year>1989 & /*
		*/(Awindow==1),fe i(cow) cluster(cow)
		lincom post-pre
		tab Acoup if e(sample)
		est store repress3
		qui: xi: xtreg repress i.period postNcoup preNcoup  if year>1989 &  /*
		*/(Nwindow==1),fe i(cow) cluster(cow)
		lincom post-pre
		tab Ncoup if e(sample)
		est store repress4
		qui: xi: xtreg repress i.period postFcoup preFcoup  if  year>1989 & /*
		*/(Fwindow==1),fe i(cow) cluster(cow)
		lincom post-pre
		tab Fcoup if e(sample)
		est store repress5
		
		tempfile g1 g2 g3 g4
		estimates restore repress5
		nlcom _b[postFcoup]-_b[preFcoup], post level(95) 
		parmest,label saving(`g1',replace) level(95)
		estimates restore repress3
		nlcom _b[postAcoup]-_b[preAcoup], post level(95) 
		parmest,label saving(`g2',replace) level(95) 
		estimates restore repress2
		nlcom _b[postDcoup]-_b[preDcoup], post level(95) 
		parmest,label saving(`g3',replace) level(95) 
		estimates restore repress4
		nlcom _b[postNcoup]-_b[preNcoup], post level(95) 
		parmest,label saving(`g4',replace) level(95) 
		dsconcat `g1' `g2' `g3' `g4'
		replace parm="F" if _n==1
		replace parm="A" if _n==2
		replace parm="D" if _n==3
		replace parm="N" if _n==4
		sencode parm, gen(myparm)
		drop if _n>4
		replace estimate = estimate*100
		replace min95 = min95*100
		replace max95 = max95*100
		gen est = round(estimate, .1)
		eclplot estimate min max myparm if _n<=4, rplottype() title(1990-2010, size(medlarge)) /*
		*/ yline(0,lcolor(gs10) lpattern(dash)) xscale(range(0.75 4.25))  /*
		*/ xlab(1 2 3 4) xtitle("Political change after coup event",height(6)) ytitle("(Post-coup) - (Pre-coup) repression") /*
		*/ plotregion(margin(large)) estopts(mlabel(est) mlabposition(8)) scheme(lean2) ylab(-80 (20) 80,glcolor(gs16)) /*
		*/ legend(on lab(1 "95% ci") lab (2 "estimate") pos(6) ring(2) col(2) size(small)) saving(t2, replace)
	 	estout repress* using C2.tex, cells(b(star  fmt(%9.3f)) se(par fmt(%9.2f))) stats(ll r2 N) style(tex) replace label starlevels(+ 0.10 * 0.05 ** 0.01)

	graph combine t1.gph t2.gph,  xsize(7) ysize(4) graphregion(color(white))
	graph export "C:\Users\jwright\Documents\My Dropbox\Research\Coups\DFGW\Manuscript files\golden\yearly-repression.pdf", as(pdf) replace
		
**************************************
*** Appendix: Dict-dict robustness ***
**************************************
	use temprepress, clear

	* RE *
	 qui: xi: xtreg repress i.period postDcoup preDcoup  if m>=3 & /*
	*/(Dwindow==1), i(cow) cluster(cow)
	lincom post-pre
	est store r1
	 qui: xi: xtreg repress i.period postAcoup preAcoup  if m>=3 & /*
	*/(Awindow==1), i(cow) cluster(cow)
	lincom post-pre
	est store r2

	* No unit effect *
	 qui: xi: reg repress i.period postDcoup preDcoup  if m>=3 & /*
	*/(Dwindow==1),  cluster(cow)
	lincom post-pre
	est store r3
	 qui: xi: reg repress i.period postAcoup preAcoup  if m>=3 & /*
	*/(Awindow==1), cluster(cow)
	lincom post-pre
	est store r4
	
	* Year FE *  
	 qui: xi: xtreg repress i.year postDcoup preDcoup  if m>=3 & /*
	*/(Dwindow==1),fe i(cow) cluster(cow)
	lincom post-pre
	est store r5
	 qui: xi: xtreg repress i.year postAcoup preAcoup  if m>=3 & /*
	*/(Awindow==1),fe i(cow) cluster(cow)
	lincom post-pre
	est store r6

	* Cold war interaction *
	gen coldwar = year<1990
	local i = "postAcoup preAcoup postDcoup preDcoup"
	foreach c of local i {
		gen PCW`c' = `c'
		recode PCW`c' (1=0) if year<1990
		gen CW`c' = `c'
		recode CW`c' (1=0) if year>1989
	}
	 qui: xi: xtreg repress i.period CWpreD CWpostD PCWpreD PCWpostD  if m>=3 & /*
	*/(Dwindow==1), i(cow) cluster(cow)
	lincom PCWpost-PCWpre
	lincom CWpost-CWpre
	est store r7
	 qui: xi: xtreg repress i.period CWpreA CWpostA PCWpreA PCWpostA  if m>=3 & /*
	*/(Awindow==1), i(cow) cluster(cow)
	lincom PCWpost-PCWpre
	lincom CWpost-CWpre
	est store r8
	estout r1 r2 r3 r4 r5 r6 r7 r8 using C3.tex, cells(b(star  fmt(%9.3f)) se(par fmt(%9.2f))) stats(ll r2 N) style(tex) replace label starlevels(+ 0.10 * 0.05 ** 0.01)
	

***************************************
*** Appendix: pooled specifications ***
***************************************
	* pooled specification treats all failed coup years as omitted category *
	use temprepress, clear

	* all years, pool all coup events types, with FEs *
	qui: xi: xtreg repress i.period postAcoup preAcoup postDcoup preDcoup postNcoup preNcoup postFcoup preFcoup /*
	*/ Dcoup Acoup Ncoup if (Dwindow==1 | Awindow==1 | Nwindow==1 | Fwindow==1), i(cow) fe cluster(cow)
	lincom postA-preA
	lincom postD-preD
	lincom postN-preN
	lincom postF-preF
	est store pool1
 
	* all years, pool all coup events, random effect *
	qui: xi: xtreg repress i.period postAcoup preAcoup postDcoup preDcoup postNcoup preNcoup postFcoup preFcoup /*
	*/ Dcoup Acoup Ncoup if (Dwindow==1 | Awindow==1 | Nwindow==1 | Fwindow==1), i(cow) re cluster(cow)
	lincom postA-preA
	lincom postD-preD
	lincom postN-preN
	lincom postF-preF
	est store pool2

	* 1990-2010, pool all coup events types, with FE *
	qui: xi: xtreg repress i.period postAcoup preAcoup postDcoup preDcoup postNcoup preNcoup postFcoup preFcoup /*
	*/ Dcoup Acoup Ncoup  if (Dwindow==1 | Awindow==1 | Nwindow==1 | Fwindow==1) & year>1989, i(cow) fe cluster(cow)
	lincom postA-preA
	lincom postD-preD
	lincom postN-preN
	lincom postF-preF
	est store pool3

	* 1990-2010, pool all coup events, random effect *
	qui: xi: xtreg repress i.period postAcoup preAcoup postDcoup preDcoup postNcoup preNcoup postFcoup preFcoup /*
	*/  Dcoup Acoup Ncoup  if (Dwindow==1 | Awindow==1 | Nwindow==1 | Fwindow==1) & year>1989, i(cow) re cluster(cow)
	lincom postA-preA
	lincom postD-preD
	lincom postN-preN
	lincom postF-preF	
	est store pool4
	
		tempfile g1 g2 g3 g4
		estimates restore pool1
		nlcom _b[postFcoup]-_b[preFcoup], post level(95) 
		parmest,label saving(`g1',replace) level(95)
		estimates restore pool1
		nlcom _b[postAcoup]-_b[preAcoup], post level(95) 
		parmest,label saving(`g2',replace) level(95) 
		estimates restore pool1
		nlcom _b[postDcoup]-_b[preDcoup], post level(95) 
		parmest,label saving(`g3',replace) level(95) 
		estimates restore pool1
		nlcom _b[postNcoup]-_b[preNcoup], post level(95) 
		parmest,label saving(`g4',replace) level(95) 
		dsconcat `g1' `g2' `g3' `g4'
		replace parm="F" if _n==1
		replace parm="A" if _n==2
		replace parm="D" if _n==3
		replace parm="N" if _n==4
		sencode parm, gen(myparm)
		drop if _n>4
		replace estimate = estimate*100
		replace min95 = min95*100
		replace max95 = max95*100
		gen est = round(estimate, .1)
		eclplot estimate min max myparm if _n<=4, rplottype() title(1950-2010, size(medlarge)) /*
		*/ yline(0,lcolor(gs10) lpattern(dash)) xscale(range(0.75 4.25))  /*
		*/ xlab(1 2 3 4) xtitle("Political change after coup event",height(6)) ytitle("(Post-coup) - (Pre-coup) repression") /*
		*/ plotregion(margin(large)) estopts(mlabel(est) mlabposition(8)) scheme(lean2) ylab(-40 (20) 40,glcolor(gs16)) /*
		*/ legend(on lab(1 "95% ci") lab (2 "estimate") pos(6) ring(2) col(2) size(small)) saving(t1, replace)
		
		tempfile g1 g2 g3 g4
		estimates restore pool3
		nlcom _b[postFcoup]-_b[preFcoup], post level(95) 
		parmest,label saving(`g1',replace) level(95)
		estimates restore pool3
		nlcom _b[postAcoup]-_b[preAcoup], post level(95) 
		parmest,label saving(`g2',replace) level(95) 
		estimates restore pool3
		nlcom _b[postDcoup]-_b[preDcoup], post level(95) 
		parmest,label saving(`g3',replace) level(95) 
		estimates restore pool3
		nlcom _b[postNcoup]-_b[preNcoup], post level(95) 
		parmest,label saving(`g4',replace) level(95) 
		dsconcat `g1' `g2' `g3' `g4'
		replace parm="F" if _n==1
		replace parm="A" if _n==2
		replace parm="D" if _n==3
		replace parm="N" if _n==4
		sencode parm, gen(myparm)
		drop if _n>4
		replace estimate = estimate*100
		replace min95 = min95*100
		replace max95 = max95*100
		gen est = round(estimate, .1)
		eclplot estimate min max myparm if _n<=4, rplottype() title(1990-2010, size(medlarge)) /*
		*/ yline(0,lcolor(gs10) lpattern(dash)) xscale(range(0.75 4.25))  /*
		*/ xlab(1 2 3 4) xtitle("Political change after coup event",height(6)) ytitle("(Post-coup) - (Pre-coup) repression") /*
		*/ plotregion(margin(large)) estopts(mlabel(est) mlabposition(8)) scheme(lean2) ylab(-60 (20) 60,glcolor(gs16)) /*
		*/ legend(on lab(1 "95% ci") lab (2 "estimate") pos(6) ring(2) col(2) size(small)) saving(t2, replace)
		graph combine t1.gph t2.gph,  xsize(7) ysize(4) graphregion(color(white))
		graph export "C:\Users\jwright\Documents\My Dropbox\Research\Coups\DFGW\Manuscript files\golden\yearly-repression-pooled-fe.pdf", as(pdf) replace
		
		tempfile g1 g2 g3 g4
		estimates restore pool2
		nlcom _b[postFcoup]-_b[preFcoup], post level(95) 
		parmest,label saving(`g1',replace) level(95)
		estimates restore pool2
		nlcom _b[postAcoup]-_b[preAcoup], post level(95) 
		parmest,label saving(`g2',replace) level(95) 
		estimates restore pool2
		nlcom _b[postDcoup]-_b[preDcoup], post level(95) 
		parmest,label saving(`g3',replace) level(95) 
		estimates restore pool2
		nlcom _b[postNcoup]-_b[preNcoup], post level(95) 
		parmest,label saving(`g4',replace) level(95) 
		dsconcat `g1' `g2' `g3' `g4'
		replace parm="F" if _n==1
		replace parm="A" if _n==2
		replace parm="D" if _n==3
		replace parm="N" if _n==4
		sencode parm, gen(myparm)
		drop if _n>4
		replace estimate = estimate*100
		replace min95 = min95*100
		replace max95 = max95*100
		gen est = round(estimate, .1)
		eclplot estimate min max myparm if _n<=4, rplottype() title(1950-2010, size(medlarge)) /*
		*/ yline(0,lcolor(gs10) lpattern(dash)) xscale(range(0.75 4.25))  /*
		*/ xlab(1 2 3 4) xtitle("Political change after coup event",height(6)) ytitle("(Post-coup) - (Pre-coup) repression") /*
		*/ plotregion(margin(large)) estopts(mlabel(est) mlabposition(8)) scheme(lean2) ylab(-40 (20) 40,glcolor(gs16)) /*
		*/ legend(on lab(1 "95% ci") lab (2 "estimate") pos(6) ring(2) col(2) size(small)) saving(t1, replace)
		
		tempfile g1 g2 g3 g4
		estimates restore pool4
		nlcom _b[postFcoup]-_b[preFcoup], post level(95) 
		parmest,label saving(`g1',replace) level(95)
		estimates restore pool4
		nlcom _b[postAcoup]-_b[preAcoup], post level(95) 
		parmest,label saving(`g2',replace) level(95) 
		estimates restore pool4
		nlcom _b[postDcoup]-_b[preDcoup], post level(95) 
		parmest,label saving(`g3',replace) level(95) 
		estimates restore pool4
		nlcom _b[postNcoup]-_b[preNcoup], post level(95) 
		parmest,label saving(`g4',replace) level(95) 
		dsconcat `g1' `g2' `g3' `g4'
		replace parm="F" if _n==1
		replace parm="A" if _n==2
		replace parm="D" if _n==3
		replace parm="N" if _n==4
		sencode parm, gen(myparm)
		drop if _n>4
		replace estimate = estimate*100
		replace min95 = min95*100
		replace max95 = max95*100
		gen est = round(estimate, .1)
		eclplot estimate min max myparm if _n<=4, rplottype() title(1990-2010, size(medlarge)) /*
		*/ yline(0,lcolor(gs10) lpattern(dash)) xscale(range(0.75 4.25))  /*
		*/ xlab(1 2 3 4) xtitle("Political change after coup event",height(6)) ytitle("(Post-coup) - (Pre-coup) repression") /*
		*/ plotregion(margin(large)) estopts(mlabel(est) mlabposition(8)) scheme(lean2) ylab(-60 (20) 80,glcolor(gs16)) /*
		*/ legend(on lab(1 "95% ci") lab (2 "estimate") pos(6) ring(2) col(2) size(small)) saving(t2, replace)
		graph combine t1.gph t2.gph,  xsize(7) ysize(4) graphregion(color(white))
		graph export "C:\Users\jwright\Documents\My Dropbox\Research\Coups\DFGW\Manuscript files\golden\yearly-repression-pooled-re.pdf", as(pdf) replace
	
	* we tried treating all successful coups grouped together as the omitted category (no Dcoup, Acoup, or Ncoup in specfication) *
	* with similar slightly more efficient results */	
	use temprepress, clear
	qui: xi: xtreg repress i.period postAcoup preAcoup postDcoup preDcoup postNcoup preNcoup postFcoup preFcoup /*
	*/  if (Dwindow==1 | Awindow==1 | Nwindow==1 | Fwindow==1), i(cow) fe cluster(cow)
	lincom postA-preA
	lincom postD-preD
	lincom postN-preN
	lincom postF-preF
 	qui: xi: xtreg repress i.period postAcoup preAcoup postDcoup preDcoup postNcoup preNcoup postFcoup preFcoup /*
	*/ if (Dwindow==1 | Awindow==1 | Nwindow==1 | Fwindow==1), i(cow) re cluster(cow)
	lincom postA-preA
	lincom postD-preD
	lincom postN-preN
	lincom postF-preF
	qui: xi: xtreg repress i.period postAcoup preAcoup postDcoup preDcoup postNcoup preNcoup postFcoup preFcoup /*
	*/ if (Dwindow==1 | Awindow==1 | Nwindow==1 | Fwindow==1) & year>1989, i(cow) fe cluster(cow)
	lincom postA-preA
	lincom postD-preD
	lincom postN-preN
	lincom postF-preF
	qui: xi: xtreg repress i.period postAcoup preAcoup postDcoup preDcoup postNcoup preNcoup postFcoup preFcoup /*
	*/ if (Dwindow==1 | Awindow==1 | Nwindow==1 | Fwindow==1) & year>1989, i(cow) re cluster(cow)
	lincom postA-preA
	lincom postD-preD
	lincom postN-preN
	lincom postF-preF	
	
	
*******************************
*** Appendix: leave-one-out ***
*******************************
		**** All years: leave one out ****
		use temprepress, clear
		* Tag sample countries *
		qui: xi: xtreg repress i.period postAcoup preAcoup if (Awindow==1), i(cow) fe cluster(cow)
		lincom postA-preA
		egen ctag = tag(cow) if e(sample)
		list cow if ctag==1, clean noobs
		gen f0=.
		gen b=.
		local i=1
		local id="1 41 42 90 100 130 140 145 150 160 404 420 432 433 434 435 436 437 438 439 450 451 452 461 482 483 484 500 516 517 530 570 615 620 625 640 645 651 652 678 700 770 771 775 800 811 812 817 850"
		foreach c of local id {
			qui: xi: xtreg repress i.period postAcoup preAcoup if Awindow==1 & cow~=`c', i(cow) fe cluster(cow) 
			qui: nlcom _b[postA]-_b[preA], post level(95) 
			matrix beta =e(b)
			qui replace f0 = beta[1,1] if _n==`i'
			qui replace b = beta[1,1] if `c'==1 /*capture full sample beta; when cow==1*/
			local i = `i' +1 
		}
		local b = b  /*full sample beta*/
		gen rb = round(b, 0.001)
		local rb =rb
		sum rb
		twoway (hist f0, bin(50) scheme(lean2) ylab(0 (0) 0, glcolor(gs16)) xlabel(0 (.05) .20) title(1950-2010) /*
		*/ xline(`b', lpattern(dash)) xtitle("Estimates for Autocratic transition coup effect") saving(t1, replace) /*
		*/ legend(label(1 "Estimates excluding 1 country at a time") label(2 "Full sample estimate: 0.195") pos(11) col(1)  ring(0))) || function x = `b',xvarlab("dash line = f(x)")  

		**** Post coldwar : leave one out ****
		* Tag sample countries *
		use temprepress, clear
		qui: xi: xtreg repress i.period postAcoup preAcoup if (Awindow==1) & year>1989, i(cow) fe cluster(cow)
		lincom postA-preA
		egen ctag = tag(cow) if e(sample)
		list cow if ctag==1, clean noobs
		gen f0=.
		gen b=.
		local i=1
		local id="1 435 437 438 451 517 615 700"
		foreach c of local id {
			qui: xi: xtreg repress i.period postAcoup preAcoup if Awindow==1 & year>1989 & cow~=`c', i(cow) fe cluster(cow) 
			qui: nlcom _b[postA]-_b[preA], post level(95) 
			matrix beta =e(b)
			qui replace f0 = beta[1,1] if _n==`i'
			qui replace b = beta[1,1] if `c'==1 /*capture full sample beta; when cow==1*/
			local i = `i' +1 
		}
		local b = b  /*full sample beta*/
		gen rb = round(b, 0.001)
		local rb =rb
		sum rb
		twoway (hist f0, bin(50) scheme(lean2) ylab(0 (0) 0, glcolor(gs16)) xlabel(0 (.1) .6) title(1990-2010) /*
		*/ xline(`b', lpattern(dash)) xtitle("Estimates for Autocratic transition coup effect") saving(t2, replace) /*
		*/ legend(label(1 "Estimates excluding 1 country at a time") label(2 "Full sample estimate: 0.45") pos(11) col(1)  ring(0))) || function x = `b',xvarlab("dash line = f(x)")  
		graph combine t1.gph t2.gph,  xsize(8) ysize(4) graphregion(color(white))
		graph export "C:\Users\jwright\Documents\My Dropbox\Research\Coups\DFGW\Manuscript files\golden\yearly-repression-leave-one-out.pdf", as(pdf) replace
	
**********************************
*** Appendix: error-correction ***
**********************************
use temprepress, clear
egen caseid = group(gwf_case)
drop if caseid==.
tsset cow year
xi: xtreg d.repression i.period l.repression d.Dcoup l.Dcoup d.Acoup l.Acoup d.Ncoup l.Ncoup d.Fcoup l.Fcoup, fe i(cow) cluster(cow)
tab gwf_non if e(sample), m
xi: xtreg d.repression i.period l.repression d.Dcoup l.Dcoup d.Acoup l.Acoup d.Ncoup l.Ncoup d.Fcoup l.Fcoup if year>1989, fe i(cow) cluster(cow)

local i = "repression Dcoup Acoup Ncoup Fcoup"
foreach c of local i {
	tsset cow year
	gen d_`c' = d.`c'
}
xi: qui: ivreg repression i.cow i.period (d_repression = l.repression) d_Dcoup Dcoup d_Acoup Acoup d_Ncoup Ncoup d_Fcoup Fcoup, cluster(cow) 
est store ecmrep1
xi: qui: ivreg repression i.cow i.period (d_repression = l.repression) d_Dcoup Dcoup d_Acoup Acoup d_Ncoup Ncoup d_Fcoup Fcoup if year>1989, cluster(cow) 
est store ecmrep2
label var Dcoup "D"
label var Acoup "A"
label var Ncoup "N"
label var Fcoup "F"

 		coefplot (ecmrep1, msymbol(+))  (ecmrep2, msymbol(T)), title("Long-run repression effect", size(medium))   /*
		*/ scheme(lean2) drop(_cons _Iper* _Icow* d_*) xlab(-8 (4) 8) xline(0) grid(glcolor(gs15)) mfcolor(white) /*
		*/ order(cwcoupMA pcwcoupMA ld) ysize(4) xsize(4) mlabel format(%9.1f) mlabposition(4) mlabgap(*1) /*
		*/ legend(label(3 "1950-2010") label(6 "1990-2010")  pos(6) ring(1.5) col(3))  /*
		*/ levels(95 90) xtitle("  Estimate", height(6)) 
 		graph export "C:\Users\jwright\Documents\My Dropbox\Research\Coups\DFGW\Manuscript files\golden\yearly-repression-ecm.pdf", as(pdf) replace

		* ADL *  De Boef & Keele 2008, page * 186
		xi: qui: xtreg repress i.period i.gwf_regime l.repress Dcoup l.Dcoup Acoup l.Acoup Ncoup l.Ncoup Fcoup l.Fcoup, cluster(cow) i(cow) fe
		nlcom (_b[Dcoup] + _b[l.Dcoup])/(1-_b[l.repression])
		nlcom (_b[Acoup] + _b[l.Acoup])/(1-_b[l.repression])
		nlcom (_b[Ncoup] + _b[l.Ncoup])/(1-_b[l.repression])
		nlcom (_b[Fcoup] + _b[l.Fcoup])/(1-_b[l.repression])
		lincom l.repress
		xi: qui: ivreg repression i.cow i.period i.gwf_regime (d_repression = l.repression) d_Dcoup Dcoup d_Acoup Acoup d_Ncoup Ncoup d_Fcoup Fcoup, cluster(cow) 
		lincom Dcoup
		lincom Acoup
		lincom Ncoup
		lincom Fcoup
 
*******************************************************
*** Appendix: uncertainty in the repression measure ***
*******************************************************




/* R code to get 1000 sims of the latent variable using Fariss' mean and sd 

	 # (1) use HumanRightsProtectionScores_v2.03 in the working directory
	 # (2) use the R code to generate 1000 simulated point estimates for each obs with a mean and sd for the latent estiamte
	 # (3) R code puts .csv files in same folder that can be merged with autocratic breakdown data prior to analysis
          
						rm(list=ls())
						set.seed(23789)
						setwd("c:/Users/jwright/Documents/My Dropbox/Research/Coups/DFGW/Data files")
						need.to.install<-FALSE
						if(need.to.install){
						  install.packages("car")
						  install.packages("plyr")
						  install.packages("lattice")
				
						}
						library(car)
						library(foreign)
						library(plyr)
						library(lattice)
						simnum<-1000
						names<-matrix(, nrow = 1, ncol = simnum+2)				# to store column names
						names[1,1]<-"cowcode"
						names[1,2]<-"year"
						data.fariss<-read.csv("HumanRightsProtectionScores_v2.03.csv")    # correctly input the data set from .csv  
						data.hr <- data.fariss[c(1:2,18:19)] 
						nrdf<-nrow(data.hr)
						max<-(ncol(data.hr)-2)/2							# number of latent measures: 1 in this application
						
						for(j in 1:max){
							q<-j
							n<-matrix(, nrow = nrdf, ncol = simnum+2)
							n[,1]<-data.hr[,1] 							# cowcode
							n[,2]<-data.hr[,2]							# year
							for(i in 1:simnum){
								if(q==1) {
									n[,i+2]<-rnorm(1:nrow(data.hr), mean= data.hr$latentmean, sd=data.fariss$latentsd)
								}	
								d<-i
								toString(d)
								x <- c("sim", d)
								x<-paste(x, collapse="")
								names[1,i+2]<-x											# add column names by i
							}
							dimnames(n)<-list(c(), c(names))
							if(q==1) {
								n<-write.csv(n,"f1.csv")
							}
						}


*/ 
global r = 1000
set matsize 1500
insheet using "f1.csv",  clear
recode cow (679=678)
sort cow year
drop if year==year[_n-1] & cow==cow[_n-1]
sort cow year
merge cow year using temprepress, 
tab _merge
save temp, replace

global r = 1000
local c = "F A D N"
foreach g of local c {
	use temp, clear
	matrix sims`g' = J($r,1,.)
	qui: xi: xtreg repress i.period post`g'coup pre`g'coup  if m>=3 & (`g'window==1),fe i(cow) cluster(cow)
	lincom post`g' - pre`g'
	keep if e(sample)
	forvalues i = 1(1)$r {
		qui: replace sim`i' = sim`i'*-1  /* flip scale to measure repression instead of human rights respect */
		qui: xi: xtreg sim`i' i.period post`g'coup pre`g'coup  if m>=3 & (`g'window==1),fe i(cow) cluster(cow)
		qui: nlcom _b[post`g'coup]-_b[pre`g'coup], post level(95)
		qui: matrix sims`g'[`i',1]= e(b)
	}
}

clear
set obs $r
gen l=.
gen u=.
gen e=.
local t = 1
local c = "F A D N"
foreach g of local c {	
	qui gen rpress`g' =.
	forvalues i = 1(1)$r { 
		local z = sims`g'[`i',1]
		qui replace rpress`g'=`z' if _n==`i'
	}
	centile rpress`g', centile(0.5,1,2.5,97.5,99,99.5) normal
	centile rpress`g', centile(2.5, 50, 97.5)
	matrix hi`g' = r(c_1)
	matrix lo`g' = r(c_3)
	matrix est`g' = r(c_2)
	replace l =  lo`g'[1,1] if _n==`t'
	replace u =  hi`g'[1,1] if _n==`t'
	replace e = est`g'[1,1] if _n==`t'
	local t = `t' + 1
}
gen est = round(e, 0.01)
gen n = _n
set scheme lean2
graph twoway (scatter e n if _n<5, mlabel(est) mlabposition(5) plotregion(margin(small))  /*
	*/ yline(0,lcolor(gs10) lpattern(dash)) ylabel(-.6(.2).4,glcolor(gs15)) /*
	*/ title("Repression estimates with uncertainty in repression measure", tstyle(size(medium)))) /*
	*/ (rspike l u n if _n<5, ytitle("Repression estimates", height(-4)) xtitle("")), xtitle("Political change after coup event",height(6))  /*
	*/ yscale(range(-.4 .4)) xscale(range(0.7 4.3)) xlab(1 "F" 2 "A" 3 "D" 4 "N")  /*
	*/ legend( label(1 "Median") label(2 "95% CI (2-tailed)") pos(6) col(2) ring(1))
 graph export "C:\Users\jwright\Documents\My Dropbox\Research\Coups\DFGW\Manuscript files\golden\yearly-repression-uncertainty.pdf", as(pdf) replace

 


		
********************** The end ***************************

log close
